
# -*- coding: utf8 -*-
from flask import Flask
from flask import send_file
import numpy as np
from extract_data import Data

'''just for test'''

''''全国KFC门店城市分布数量图, data form YumChina'''
from collections import Counter
from pyecharts import Geo

#数据定义
CITY_COLUMNS = ["store_name", "city", "store_numb", "longitude", "latitude"]
STORE_COLUMNS =["Storename", "City","StoreNumb","longitude","latitude"]
NEW_COLUMNS = ["STORE_NAEM", "STORE_CITY", "STORE_CODE", "LONGITUDE", "LATITUDE"]

data = Data()
raw_data = data.get_raw_data()
data.send_data(raw_data)
raw_dataset = data.get_data()
raw_dataset = raw_dataset.replace('NULL', np.NaN).dropna()


def get_city_data(data):

    data.columns = NEW_COLUMNS
    #部分省市县重命名
    data.STORE_CITY[(data.STORE_CITY == "省直辖县") & (data.LATITUDE < '20')] = '海南省直辖县'
    data.STORE_CITY[(data.STORE_CITY == "省直辖县") & (data.LATITUDE > '20')] = '湖北省直辖县'
    data.STORE_CITY[(data.STORE_CITY == "省(自治区)直辖县")] = '石河子直辖县'
    return data

def get_store_data(data):
    store = data
    store.columns = NEW_COLUMNS
    storename = np.array(store.STORE_NAEM)
    storenumb = np.array(store.STORE_CODE)
    Storeinfo = storename + storenumb
    store["STOREINFO"] = Storeinfo
    Store = store[["STOREINFO", "LONGITUDE", "LATITUDE"]]
    return Store


def store_list_generator(dataset):
    Store_np = np.array(dataset)
    Store_list = Store_np.tolist()
    return Store_list

#生成绘图数据，城市名称和门店数量的映射
def city_dict_generator(dataset):

    city = Counter(dataset.STORE_CITY)
    # city_dict = dict(city)
    return city

def store_dict_generator(dataset):
    store = Counter(dataset.STOREINFO)
    store_dict = dict(store)
    return store_dict


def city_draw_graph(city_dict):
    geo = Geo("全国各城市KFC门店数量分布图", "data from YumChina", title_pos="center", width=1600,
              height=800)
    attr, value = geo.cast(city_dict)

    #加入库内不存在的城市的坐标：
    geo.add_coordinate('黔南布依族苗族自治州', 104.919227, 25.098920)
    geo.add_coordinate('黔西南布依族苗族自治州', 104.918640, 25.097109)
    geo.add_coordinate('菏泽市', 116.109932, 34.777239)
    geo.add_coordinate('普洱市', 100.971409, 22.777727)
    geo.add_coordinate('嘉峪关市 ', 98.27, 39.80)
    geo.add_coordinate('海南省直辖县', 110.758947, 19.606585)
    geo.add_coordinate('湖北省直辖县', 113.448870, 30.368365)
    geo.add_coordinate('襄阳市', 111.642317, 32.262862)
    geo.add_coordinate('石河子直辖县', 86.069285, 44.301701)

    geo.add("", attr, value, visual_split_number=6, visual_text_color="#fff",
            symbol_size=12, is_visualmap=True, show=False)

    geo.render(path='store_city.html')

def store_draw_graph(store_dict, store_list):
    geo = Geo("全国KFC门店分布", "data from YumChina",title_pos="center", width=1800,
          height=900)
    attr, value = geo.cast(store_dict)

    for i in range(len(store_list)):
        geo.add_coordinate(store_list[i][0],store_list[i][1],store_list[i][2])
    geo.add("", attr,value,maptype='china',symbol_size=5,is_roam=False,type="scatter",is_visualmap=True)
    geo.render(path='store_distribute.html')


app = Flask(__name__)
@app.route('/')
def app_0():
    return "<h1>YumChina 数据</h1>  <br> <a href='/one'>各城市门店数量</a> <br> " \
           "<a href='/two'>所有门店分布数据</a>"

@app.route('/one')
def city_number():

    city_data = get_city_data(raw_dataset)
    city_dict = city_dict_generator(city_data)
    city_draw_graph(city_dict)
    return send_file("store_city.html")


@app.route('/two')
def store_distribute():

    store_data = get_store_data(raw_dataset)
    store_dict = store_dict_generator(store_data)
    store_list = store_list_generator(store_data)
    store_draw_graph(store_dict, store_list)
    return send_file("store_distribute.html")


app.run(host='0.0.0.0')
